package daily_exercise;

public class get_next {

    public static int[] getNext(char[] arr){
        if (arr == null || arr.length == 0){
            return null;
        }
        int[] res = new int[arr.length];
        //res[0] = 0;
        for (int i = 1; i < arr.length; i++) {
            if (res[i-1] != 0){
                int temp = arr[i] == arr[0] ? 1: 0;
                res[i] = (arr[i] == arr[res[i-1]]) ? res[i-1] + 1 : temp;
            } else { // res[i-1]==0
                res[i] = (arr[i] == arr[0]) ? 1 : 0;
            }
        }
        return res;
    }

    public static void main(String[] args) {
        String s1 = "aaaab";
        char[] chas1 = s1.toCharArray();
//        String s1 = "ABABCABAA";
//        char[] chas1 = s1.toCharArray();
        int[] res = getNext(chas1);
        for (int i = 0; i < res.length; i++) {
            System.out.println(res[i]);
        }
    }
}
